package eu.smoothit.sis.controller.iop.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import eu.smoothit.sis.controller.iop.ConfigParams;
import eu.smoothit.sis.controller.iop.IPRange;
import eu.smoothit.sis.controller.iop.IoPConfig;
import eu.smoothit.sis.db.SisDAOFactory;
import eu.smoothit.sis.db.api.daos.IComponentConfigDAO;
import eu.smoothit.sis.db.api.daos.IPRangeDAO;
import eu.smoothit.sis.db.impl.entities.ComponentConfigEntry;
import eu.smoothit.sis.db.impl.entities.IPRangeConfigEntry;
import eu.smoothit.sis.init.web.SisWebInitializer;

@Stateless
public class IoPConfigImpl implements IoPConfig {
	
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public ConfigParams getIoPConfigParams(String ipAddress){
		
		ConfigParams res = new ConfigParams();
	
		SisDAOFactory factory = SisDAOFactory.getFactory();
		IComponentConfigDAO dao = factory.createComponentConfigDAO();
		
		List<ComponentConfigEntry> configList = dao.findByComponent(SisWebInitializer.COMPONENT_NAME_CONTR_IOP);
		
		boolean flag = false;
		
		if (configList != null && configList.size() > 0){
			for (ComponentConfigEntry entry: configList){
				if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_OPERATION_MODE)){
					res.setMode((String) entry.getValue());
					if (((String) entry.getValue()).equalsIgnoreCase(SisWebInitializer.VALUE_IOP_OPERATION_MODE_COLLAB))
						flag=true;
				}
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_LOCAL_IP_RANGES)){
					List<IPRange> tmplist = new ArrayList<IPRange>();
					
					IPRangeDAO ipdao = SisDAOFactory.getFactory().createIPRangeDAO();
					IPRangeConfigEntry iprange = new IPRangeConfigEntry();
					iprange.setLocal(true);
					List<IPRangeConfigEntry> localIPs = ipdao.get(iprange);
					if (localIPs != null && localIPs.size() > 0)
						for(IPRangeConfigEntry i : localIPs) {
							IPRange tmp = new IPRange();
							tmp.setPrefix(i.getPrefix());
							tmp.setPrefix_length(i.getPrefix_len());
							tmplist.add(tmp);
						}
					
					res.setLocalIPRangess(tmplist);
				}
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_REMOTE_CONNECTION_FLAG))
					res.setRemotes(((Boolean) entry.getValue()).booleanValue());
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_UNCHOKING_SLOTS))
					res.setSlots(((Integer) entry.getValue()).intValue());
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_SIS_SWARM_SELECTION_T)
						&& (flag == true))
					res.setT(((Integer) entry.getValue()).intValue());
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_SIS_SWARM_SELECTION_ULOW)
						&& (flag == true))
					res.setUlow(((Float)entry.getValue()).floatValue());
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_SIS_SWARM_SELECTION_DLOW)
						&& (flag == true))
					res.setDlow(((Float)entry.getValue()).floatValue());
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_SIS_SWARM_SELECTION_U)
						&& (flag == true))
					res.setU(((Float)entry.getValue()).floatValue());
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_SIS_SWARM_SELECTION_D)
						&& (flag == true))
					res.setD(((Float)entry.getValue()).floatValue());
				else if (entry.getPropName().equalsIgnoreCase(SisWebInitializer.PARAM_IOP_SIS_SWARM_SELECTION_X)
						&& (flag == true))
					res.setX(((Float)entry.getValue()).floatValue());
			}
			
			return res;
		} else
			return null;
		
	}
	

}
